package ipgather.core.host.thread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.net.URLConnection;

public class HelloWorld {
	private static final int[] PORTS = {80,8080,8000,8081,8088,9000,8888,8123};
	static URL URL = null;
	public static void main(String[] args) {
		
		
		HelloWorld testThread= new HelloWorld();
		
		testThread.checkProxyHost("121.69.30.2", 8118);
		//238.244.98.62:3128
	}
	
	
	static{
		try {
			URL = new URL("http://www.haosou.com/");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	public boolean checkProxyHost(String host,int port){
		InetSocketAddress proxyAddr = new InetSocketAddress(host, port);
		boolean p = false;
		try {
			URLConnection connection = URL.openConnection(new Proxy(Type.HTTP, proxyAddr));
			connection.setConnectTimeout(10000);
			String content = convertStreamToString(connection.getInputStream());
			System.out.println(content);
			if(0!=connection.getDate()&&content.contains("好搜 — 用好搜，特顺手")){
				p = true;
				System.out.println(host+":"+port+"->success");
			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
			//e.printStackTrace();
		}
		return p;
	}
	
	private String convertStreamToString(InputStream is) {
		if (null == is){
			return "";
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}
	
}
